package com.yingxue.lesson.service.impl;

import com.yingxue.lesson.entity.SysUser;
import com.yingxue.lesson.entity.User;
import com.yingxue.lesson.mapper.SysUserMapper;
import com.yingxue.lesson.service.UserService;
import com.yingxue.lesson.utils.PasswordUtils;
import com.yingxue.lesson.vo.req.RegisterReqVO;
import com.yingxue.lesson.vo.req.UpdateUserReqVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

/**
 * @Author gusheng
 * @Date 2021/1/5 14:49
 * @ClassName 类名称
 * @Description 类描述
 * @Version 3.4
 */
@Service
public class UserServiceImpl implements UserService {

    public static final String CACHE_KEY_USER = "user:";

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RedisTemplate redisTemplate;



    @Override
    public void createUser(User user) {
        this.sysUserMapper.insertSelectiveUser(user);
        //缓存key
        String key = CACHE_KEY_USER+user.getId();
        //去数据库捞数据缓存
        user = this.sysUserMapper.selectByPrimaryKey2(user.getId());
        redisTemplate.opsForValue().set(key,user);
    }

    @Override
    public void updateUser(User user) {
       int i = this.sysUserMapper.updateByPrimaryKeyUser(user);
       if(i==1){
           //更新数据库成功，更新缓存
           user = this.sysUserMapper.selectByPrimaryKey2(user.getId());
           String key = CACHE_KEY_USER+user.getId();
           redisTemplate.opsForValue().set(key,user);
       }
    }

    @Override
    public User findUserById(int id) {
        //先找缓存
        User u = (User) redisTemplate.opsForValue().get(CACHE_KEY_USER + id);
        if(u==null){
            //找不到再去数据库找
            u = this.sysUserMapper.selectByPrimaryKey2(id);
            //在缓存
            redisTemplate.opsForValue().set(CACHE_KEY_USER+id,u);
        }
        return u;
    }



    @Override
    public SysUser getUserInfo(String id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public String register(RegisterReqVO vo) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(vo,sysUser);
        sysUser.setSalt(PasswordUtils.getSalt());
        String encode = PasswordUtils.encode(vo.getPassword(),sysUser.getSalt());
        sysUser.setPassword(encode);
        sysUser.setId(UUID.randomUUID().toString());
        sysUser.setCreateTime(new Date());
        int i = sysUserMapper.insertSelective(sysUser);
        if(i!=1){
            return "注册失败";
        }
        return "注册成功";
    }

    @Override
    @Transactional
    public String updateUserInfo(UpdateUserReqVO vo) {
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(vo.getId());
        if(null==sysUser){
            return "更新失败";
        }
        SysUser update = new SysUser();
        BeanUtils.copyProperties(vo,update);
        int count = sysUserMapper.updateByPrimaryKeySelective(update);
        if(count!=1){
            return "操作失败";
        }
       // int i=1/0;
        return "操作成功";
    }

    @Override
    public String deleted(String id) {
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser.setUpdateTime(new Date());
        sysUser.setDeleted(1);
        int count = sysUserMapper.updateByPrimaryKeySelective(sysUser);
        if(count==0){
            return "删除失败";
        }
        return "删除成功";
    }

}
